package com.noble.meether.logic.third.auth;

import android.support.v4.app.FragmentActivity;

import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.log.PLogger;
import com.noble.library.log.PToast;
import com.noble.library.third.ThirdConstant;
import com.noble.library.third.auth.OauthCallback;
import com.noble.library.third.auth.ThirdInfo;

/**
 * 三方结果回调
 * Created by Su on 2017/10/13.
 */
public class OauthBack implements OauthCallback {
    private FragmentActivity activity;

    public OauthBack(FragmentActivity activity) {
        this.activity = activity;
    }

    @Override
    public void onSuccess(int platform, final ThirdInfo user) {
        PLogger.d("Third---onSuccess: " + platform + " " + user.getAvatar() + ", " + user.getNickName());
        LoadingDialog.show(activity, "正在登录");

        ThirdConstant.Third_NickName = user.getNickName();
        // 微信登录
        if (platform == ThirdConstant.WECHAT) {
            ThirdConstant.Third_Avatar = user.getAvatar();
            ModuleMgr.getLoginMgr().weChatLogin(user.getUnionid(), response -> UIShow.showRegInfoAct((FragmentActivity) App.activity));
            return;
        }

        // QQ登录
        if (platform == ThirdConstant.QQ) {
            ThirdConstant.Third_Avatar = user.getAvatarLarge();
            ModuleMgr.getLoginMgr().qqLogin(user.getOpenid(), response -> UIShow.showRegInfoAct((FragmentActivity) App.activity));
        }

    }

    @Override
    public void onFailure(int platform, String msg) {
        PLogger.d("Third----onFailure: " + platform + " " + msg);
        LoadingDialog.closeLoadingDialog();
        PToast.showShort("登录失败");
    }

    @Override
    public void onCancelled(int platform) {
        PLogger.d("Third----取消登录: " + platform);
        LoadingDialog.closeLoadingDialog();
    }

    @Override
    public void onError(int platform, Exception ex) {
        PLogger.d("Third----onError: msg: " + platform);
        LoadingDialog.closeLoadingDialog();
        PToast.showShort("登录失败");
    }
}
